﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Asah.SiteWorx.Commerce;

public partial class SiteWorx_OrderManagement : System.Web.UI.Page
{
    private int SelectedOrderId
    {
        get
        {
            return Convert.ToInt32(Session["SelectedOrderId"].ToString());
        }
        set
        {
            Session["SelectedOrderId"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            // first need to check for incoming id.
            if (Request.QueryString["id"] != null && Request.QueryString["id"].Length > 0)
                LoadOrderDetails(Convert.ToInt32(Request.QueryString["id"].ToString()));
            else
                LoadOrders();
        }
            
    }

    private void LoadOrders()
    {
        pnlOrderList.Visible = true;
        pnlOrderDetails.Visible = false;

        rgvOrderList.DataSource = OrderList.GetPendingOrderList();
        rgvOrderList.DataBind();
    }
    
    protected void rgvOrderList_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item.ItemType == Telerik.Web.UI.GridItemType.Item || e.Item.ItemType == Telerik.Web.UI.GridItemType.AlternatingItem || e.Item.ItemType == Telerik.Web.UI.GridItemType.SelectedItem)
        {
            ProfileCommon profileCommon;
            OrderInfo orderInfo = (OrderInfo)e.Item.DataItem;

            if (orderInfo.CreatedBy != null)
            {
                profileCommon = Profile.GetProfile(orderInfo.CreatedBy.Name);
                Label lblOrderedBy = (Label)e.Item.FindControl("lblOrderedBy");
                lblOrderedBy.Text = profileCommon.DisplayName;
            }
        }
    }

    protected void rgvOrderList_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        LoadOrderDetails(Convert.ToInt32(e.CommandArgument.ToString()));
    }

    private void LoadOrderDetails(int orderId)
    {
        SelectedOrderId = orderId;

        pnlOrderList.Visible = false;
        pnlOrderDetails.Visible = true;

        OrderInfo orderInfo = OrderList.GetOrderInfo(SelectedOrderId);

        if (orderInfo != null)
        {
            lblOrderNumber.Text = orderInfo.ID.ToString();
            lblOrderStatus.Text = orderInfo.Status.ToString();

            //Build shipping information string.
            System.Text.StringBuilder shippingDetails = new System.Text.StringBuilder();

            shippingDetails.Append(orderInfo.ShipToInfo.Company + "<br>");
            shippingDetails.Append(orderInfo.ShipToInfo.FirstName + " " + orderInfo.ShipToInfo.LastName + "<br>");
            shippingDetails.Append(orderInfo.ShipToInfo.Address1 + "<br>");
            shippingDetails.Append(orderInfo.ShipToInfo.Address2 + "<br>");
            shippingDetails.Append(orderInfo.ShipToInfo.City + " " + orderInfo.ShipToInfo.State + ", " + orderInfo.ShipToInfo.Zip + "<br>");
            shippingDetails.Append(orderInfo.ShipToInfo.Phone + "<br>");
            shippingDetails.Append("<a href=\"mailto:" + orderInfo.ShipToInfo.Email + "\">" + orderInfo.ShipToInfo.Email + "</a><br>");

            lblShippingAddressInfo.Text = shippingDetails.ToString();
            lblShippingMethod.Text = orderInfo.ShipToInfo.ShippingMethod.ToString();

            //Build shipping information string.
            System.Text.StringBuilder billingDetails = new System.Text.StringBuilder();

            billingDetails.Append(orderInfo.BillToInfo.Company + "<br>");
            billingDetails.Append(orderInfo.BillToInfo.FirstName + " " + orderInfo.BillToInfo.LastName + "<br>");
            billingDetails.Append(orderInfo.BillToInfo.Address1 + "<br>");
            billingDetails.Append(orderInfo.BillToInfo.Address2 + "<br>");
            billingDetails.Append(orderInfo.BillToInfo.City + " " + orderInfo.BillToInfo.State + ", " + orderInfo.BillToInfo.Zip + "<br>");
            billingDetails.Append(orderInfo.BillToInfo.Phone + "<br>");
            billingDetails.Append("<a href=\"mailto:" + orderInfo.BillToInfo.Email + "\">" + orderInfo.BillToInfo.Email + "</a><br>");

            lblBillingAddressInfo.Text = billingDetails.ToString();

            lblCardType.Text = orderInfo.PaymentDetailInfo.PaymentMethod.ToString();
            lblNameOnCard.Text = orderInfo.PaymentDetailInfo.NameOnCard;
            lblCardNumber.Text = "xxxx xxxx xxxx xxxx";
            lblExpirationMonth.Text = orderInfo.PaymentDetailInfo.ExpirationMonth;
            lblExpirationYear.Text = orderInfo.PaymentDetailInfo.ExpirationYear;

            rgvOrderItems.DataSource = orderInfo.OrderItemList;
            rgvOrderItems.DataBind();

            ProfileCommon profileCommon;
            litOrderUpdateSection.Text = "";

            if (orderInfo.CreatedDate.Length > 0)
            {
                litOrderUpdateSection.Text += "Created at " + orderInfo.CreatedDate;
                if (orderInfo.CreatedBy != null)
                {
                    profileCommon = Profile.GetProfile(orderInfo.CreatedBy.Name);
                    lblTaxId.Text = profileCommon.TaxId;
                    if (profileCommon.DisplayName.Length > 0)
                        litOrderUpdateSection.Text += " by <a href=\"mailto:" + orderInfo.CreatedBy.Email + "\">" + profileCommon.DisplayName + "</a>";
                    else
                        litOrderUpdateSection.Text += " by <a href=\"mailto:" + orderInfo.CreatedBy.Email + "\">" + orderInfo.CreatedBy.Name + "</a>";
                }
            }

            if (orderInfo.LastUpdatedDate.Length > 0)
            {
                litOrderUpdateSection.Text += "<br/>Last updated at " + orderInfo.LastUpdatedDate;
                if (orderInfo.LastUpdatedBy != null)
                {
                    profileCommon = Profile.GetProfile(orderInfo.LastUpdatedBy.Name);
                    if (profileCommon.DisplayName.Length > 0)
                        litOrderUpdateSection.Text += " by <a href=\"mailto:" + orderInfo.LastUpdatedBy.Email + "\">" + profileCommon.DisplayName + "</a>";
                    else
                        litOrderUpdateSection.Text += " by <a href=\"mailto:" + orderInfo.LastUpdatedBy.Email + "\">" + orderInfo.LastUpdatedBy.Name + "</a>";
                }
            }
        }
        else  // the order wasn't found.
        {
            SelectedOrderId = 0;
            LoadOrders();
        }
    }


    protected void btnGetCardNumber_Click(object sender, EventArgs e)
    {
        if (btnGetCardNumber.Text == "View")
        {
            GetCardNumber();
            btnGetCardNumber.Text = "Hide";
        }
        else
        {
            lblCardNumber.Text = "xxxx xxxx xxxx xxxx";
            btnGetCardNumber.Text = "View";
        }
    }

    private void GetCardNumber()
    {
        try
        {
            OrderInfo orderInfo = OrderList.GetOrderInfo(SelectedOrderId);

            if (User.IsInRole("Administrator"))
            {
                lblCardNumber.Text = orderInfo.PaymentDetailInfo.GetCardNumber();
            }
        }
        catch { }
    }

    protected void btnProcessed_Click(object sender, EventArgs e)
    {
        ProcessedOrder();
    }

    private void ProcessedOrder()
    {
        Order.ProcessedOrder(SelectedOrderId);
        LoadOrders();
    }

    protected void btnDenied_Click(object sender, EventArgs e)
    {
        DeniedOrder();
    }

    private void DeniedOrder()
    {
        Order.DeniedOrder(SelectedOrderId);
        LoadOrders();
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        LoadOrders();
    }

    protected void rgvOrderItems_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item.ItemType == Telerik.Web.UI.GridItemType.Footer)
        {
            OrderInfo orderInfo = OrderList.GetOrderInfo(SelectedOrderId);

            Label lblGrandTotal = (Label)e.Item.FindControl("lblGrandTotal");
            lblGrandTotal.Text = String.Format("{0:c}", orderInfo.Total);
        }
    }
}
